import config from "@/config/index.js";
import { jsonp } from '@/common/vue-jsonp.js';



/**
 * @name: uni-app获取位置信息
 * @description: 
 * @param altitude
 * @param geocode
 * @return {*}
 */
const comUniLocation = ( altitude, geocode, callBack ) => {
    uni.getLocation({
        type: "gcj02",
        altitude: altitude,
        geocode: geocode,
        success: res => {
            callBack && callBack(res);
        },
        fail: err => {
            uni.$u.toast('位置信息获取失败');
        }
    });
}



/**
 * @name: 腾讯地图地址逆解析
 * @description: 
 * @param {latitude,longitude}
 * @return {*}
 */
const tencentLatlngToAddress = (lnglat,callBack) => {
    const location = lnglat.latitude + ',' + lnglat.longitude;
    const key = config.tencentKey;
    // #ifdef H5
    const url = 'https://apis.map.qq.com/ws/geocoder/v1?get_poi=1&output=jsonp&poi_options=radius=5000;policy=4';
    jsonp( url,{ location:location, key:key } ).then(res => {
        if( res.status == 0 ){
            callBack && callBack(res.result);
        }else{
            uni.$u.toast(res.message);
        }
    }).catch(err => {
        uni.$u.toast('逆地址解析失败');
    });
    // #endif

    // #ifdef MP-WEIXIN || APP-PLUS
    uni.request({
        url: "https://apis.map.qq.com/ws/geocoder/v1/?location="+location+"&get_poi=1&key="+key,
        data: {},
        header: { 'Content-Type': 'application/json; charset=utf-8' },
        success: res => {
            callBack && callBack(res.data.result);
        },
        fail: () => {
            uni.$u.toast('逆地址解析失败');
        }
    }) 
    // #endif
}



/**
 * @name: 腾讯地图地址解析
 * @description: 
 * @param address
 * @return {*}
 */
const tencentAddressToLatlng = (address,callBack) => {
    const key = config.tencentKey;
    // #ifdef H5
    const url = 'https://apis.map.qq.com/ws/geocoder/v1?get_poi=1&output=jsonp&poi_options=page_size=1;page_index=1';
    jsonp(url,{ address:address,key:key }).then( res => {
        if( res.status == 0 ){
            callBack && callBack(res.result);
        }else{
            uni.$u.toast(res.message);
        }
    }).catch(err => {
        uni.$u.toast('地址解析失败');
    });
    // #endif

    // #ifdef MP-WEIXIN || APP-PLUS
    uni.request({
        url: "https://apis.map.qq.com/ws/geocoder/v1/?address="+address+"&key="+key,
        data: {},
        header: { 'Content-Type': 'application/json; charset=utf-8' },
        success: res => {
            if( res.data.status == 0 ){
                callBack && callBack(res.data.result);
            }else{
                uni.$u.toast(res.data.message);
            }
        },
        fail: () => {
            uni.$u.toast('地址解析失败');
        }
    })
    // #endif
}



/**
 * @name: 导航
 * @description: 
 * @param {latitude,longitude,name}
 * @return {*}
 */
const comNavigation = (data) => {
    // #ifdef APP-PLUS || APP-NVUE
    let url = ""
    if ( plus.os.name == "Android" ) {
        plus.nativeUI.actionSheet({ 
            title: "选择地图应用",
            cancel: "取消",
            buttons: [
                { title: "腾讯地图" },
                { title: "百度地图" },
                { title: "高德地图" }
            ]
        }, function (e) {
            switch (e.index) {
                case 1:
                    url = `qqmap://map/geocoder?coord=${data.latitude},${data.longitude}&referer=${config.tencentKey}`; 
                    break;
                case 2:
                    url = `baidumap://map/marker?location=${data.latitude},${data.longitude}&title=${data.name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
                    break;
                case 3:
                    url = `androidamap://viewMap?sourceApplication=appname&poiname=${data.name}&lat=${data.latitude}&lon=${data.longitude}&dev=0`;
                    break;
            }
            if (url) {
                url = encodeURI(url);
                // 调起手机APP应用
                plus.runtime.openURL(url, function (e) {
                    plus.nativeUI.alert("本机未安装指定的地图应用");
                });
            }
        })
    } else {
        plus.nativeUI.actionSheet({
            title: "选择地图应用",
            cancel: "取消",
            buttons: [
                { title: "腾讯地图" },
                { title: "百度地图" },
                { title: "高德地图" }
            ]
        }, function (e) {
            switch (e.index) {
                case 1:
                    url = `qqmap://map/geocoder?coord=${data.latitude},${data.longitude}&referer=${config.tencentKey}`;
                    break;
                case 2:
                    url = `baidumap://map/marker?location=${data.latitude},${data.longitude}&title=${data.name}&content=${data.name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
                    break;
                case 3:
                    url = `iosamap://viewMap?sourceApplication=applicationName&poiname=${data.name}&lat=${data.latitude}&lon=${data.longitude}&dev=0`;
                    break;
            }
            if ( url ) {
                url = encodeURI(url);
                plus.runtime.openURL(url, function (e) {
                    plus.nativeUI.alert("本机未安装指定的地图应用");
                });
            }
        })
    }
    // #endif

    // #ifdef H5
    window.location.href =  `http://apis.map.qq.com/uri/v1/marker?marker=coord:${data.latitude},${data.longitude};addr:${data.name}`
    // #endif

    // #ifdef MP-WEIXIN 
    return uni.openLocation({
        latitude: data.latitude,
        longitude: data.longitude,
        address: data.name,
        scale: 18
    })
    // #endif
}



/**
 * @name: 阻止页面滚动
 * @description: 
 * @return {*}
 */
const forbidPageScroll = () => {
    const box = function(e){ passive: false  };
    document.body.style.overflow = 'hidden'; // 隐藏滚动条
    document.addEventListener("touchmove",box,false);
}



/**
 * @name: 允许页面滚动
 * @description: 
 * @return {*}
 */
const allowPageScroll = () => {
    const box = function(e){ passive: false };
    document.body.style.overflow = ''; // 出现滚动条
    document.removeEventListener("touchmove",box,false);
}



module.exports = {
    comUniLocation,
    tencentLatlngToAddress,
    tencentAddressToLatlng,
    comNavigation,
    forbidPageScroll,
    allowPageScroll,
}